import pydoc
from turtle import *
import turtle
import time
playr1 = turtle.Turtle()
playr1.hideturtle()
screen1 = turtle.Screen()
turtle.screensize(800, 600, "pink")
playr2 = turtle.Turtle()
playr2.hideturtle()
screen2 = turtle.Screen()
class Players():
    def __init__(self,player):
        self.player = player
        player.hideturtle()
        player.shape('turtle')
        player.color('yellow')
        player.turtlesize(1)
        player.penup()
        player.speed(0)
    def goto(self,x,y,head):
        self.player.goto(x,y)
        self.player.setheading(head)
class Maze(Turtle):
    size = 20
    def __init__(self, maze_list,x,y):
        Turtle.__init__(self)
        self.maze_list = maze_list
        self.hideturtle()
        self.speed(0)
        self.draw_walls(x,y)

    def draw_wall(self):
        self.pendown()
        self.begin_fill()
        self.fillcolor('red')
        for i in range(4):
            self.forward(self.size)
            self.right(90)
        self.end_fill()
        self.penup()

    # 绘制整个迷宫的墙
    def draw_walls(self,x,y):
        self.penup()
        self.goto(x, y)
        for row in range(len(self.maze_list)):
            for col in range(len(self.maze_list[0])):
                if self.maze_list[row][col] == 1:
                    self.draw_wall()
                self.goto(self.size * (col + 1) - abs(x), y - self.size * row)
            self.goto(x, y - self.size * (row + 1))
m1 = 0
n1 = 5
def up():
    global m1,n1
    if n1 >= 0 and m1 >= 0:
        if maze_list[m1-1][n1] == 0:
            playr1.setheading(90)
            playr1.forward(Maze.size)
            m1 -= 1
        print(m1,n1)
    if m1 == 12 and n1 == 7:
        text = turtle.Turtle()
        text.hideturtle()
        text.penup()
        text.goto(-125, -10)
        text.color('blue')
        text.write('you win!', font=('SimHei', 48, 'bold'))

def down():
    global m1,n1
    if n1 >= 0 and m1 >= 0:
        if maze_list[m1+1][n1] == 0:
            playr1.setheading(270)
            playr1.forward(Maze.size)
            m1 += 1
        print(m1,n1)
    if m1 == 12 and n1 == 7:
        text = turtle.Turtle()
        text.hideturtle()
        text.penup()
        text.goto(-125, -10)
        text.color('blue')
        text.write('you win!', font=('SimHei', 48, 'bold'))
def left():
    global  m1,n1
    if n1 >= 0 and m1 >= 0:
        if maze_list[m1][n1-1] == 0:
            playr1.setheading(180)
            playr1.forward(Maze.size)
            n1 -= 1
        print(m1,n1)
        if m1 == 12 and n1 == 7:
            text = turtle.Turtle()
            text.hideturtle()
            text.penup()
            text.goto(-125, -10)
            text.color('blue')
            text.write('you win!', font=('SimHei', 48, 'bold'))
def right():
    global m1, n1
    if n1 >= 0 and m1 >= 0:
        if maze_list[m1][n1+1] == 0:
            playr1.setheading(0)
            playr1.forward(Maze.size)
            n1 += 1
        print(m1,n1)
    if m1 == 12 and n1 == 7:
        text = turtle.Turtle()
        text.hideturtle()
        text.penup()
        text.goto(-125, -10)
        text.color('blue')
        text.write('you win!', font=('SimHei', 48, 'bold'))

maze_list = [
  [1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1],
  [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
  [1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1],
  [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
  [1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1],
  [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1],
  [1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1],
  [1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1],
  [1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1],
  [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1],
  [1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1],
  [1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1],
  [1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1]
]

m2 = 12
n2 = 0
def up2():
    global m2,n2
    if n2 >= 0 and m2 >= 0:
        if maze_list2[m2-1][n2] == 0:
            playr2.setheading(90)
            playr2.forward(Maze.size)
            m2 -= 1
        print(m2,n2)
        if m2 == 19 and n2 == 40:
            text = turtle.Turtle()
            text.hideturtle()
            text.penup()
            text.goto(-125, -10)
            text.color('blue')
            text.write('you win!', font=('SimHei', 48, 'bold'))

def down2():
    global m2, n2
    if n2 >= 0 and m2 >= 0:
        if maze_list2[m2 + 1][n2] == 0:
            playr2.setheading(270)
            playr2.forward(Maze.size)
            m2 += 1
        print(m2, n2)
        if m2 == 19 and n2 == 40:
            text = turtle.Turtle()
            text.hideturtle()
            text.penup()
            text.goto(-125, -10)
            text.color('blue')
            text.write('you win!', font=('SimHei', 48, 'bold'))
def left2():
    global m2, n2
    if n2 >= 0 and m2 >= 0:
        if maze_list2[m2][n2-1] == 0:
            playr2.setheading(180)
            playr2.forward(Maze.size)
            n2 -= 1
        print(m2, n2)
        if m2 == 19 and n2 == 40:
            text = turtle.Turtle()
            text.hideturtle()
            text.penup()
            text.goto(-125, -10)
            text.color('blue')
            text.write('you win!', font=('SimHei', 48, 'bold'))
def right2():
    global m2, n2
    if n2 >= 0 and m2 >= 0:
        if maze_list2[m2][n2+1] == 0:
            playr2.setheading(0)
            playr2.forward(Maze.size)
            n2 += 1
        print(m2, n2)
        if m2 == 19 and n2 == 40:
            text = turtle.Turtle()
            text.hideturtle()
            text.penup()
            text.goto(-125, -10)
            text.color('blue')
            text.write('you win!', font=('SimHei', 48, 'bold'))
maze_list2=[
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1],
    [1,1,1,0,1,0,1,0,1,0,1,0,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1],
    [1,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1],
    [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1,0,1,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,0,1,1,1,1,1,0,1,0,1,1,1,0,1,0,1,0,1,1,1],
    [1,0,1,0,1,0,1,0,1,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1],
    [1,0,1,0,1,0,1,0,1,1,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,0,1,1,1,0,1],
    [1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
]

class drawing:
    def __init__(self,name,x,y):
        self.text = turtle.Turtle()
        self.text.hideturtle()
        self.text.penup()
        self.text.goto(x, y)
        self.text.color('white')
        self.text.write(name, font=('SimHei',30 ,'bold'))
def mouse1(x,y):
    if -60 <= x <= 30 and 30 <= y<= 80:
        screen.onclick(None)
        a.text.clear()
        b.text.clear()
        c.text.clear()
        play1 = Players(playr1)
        play1.goto(-20, 120, 270)
        screen1.onkey(up, 'Up')
        screen1.onkey(down, 'Down')
        screen1.onkey(left, 'Left')
        screen1.onkey(right, 'Right')
        screen1.listen()
        Maze(maze_list, -130, 130)
        playr1.showturtle()
    elif -60 <= x <=120 and -50 <= y <= 0:
        screen.onclick(None)
        a.text.clear()
        b.text.clear()
        c.text.clear()
        play2 = Players(playr2)
        play2.goto(-400,-20, 0)
        screen2.onkey(up2, 'Up')
        screen2.onkey(down2, 'Down')
        screen2.onkey(left2, 'Left')
        screen2.onkey(right2, 'Right')
        screen2.listen()
        Maze(maze_list2, -410, 230)
        playr2.showturtle()
screen = turtle.getscreen()
screen.onclick(mouse1)
time.sleep(0.01)
a = drawing('选择难度',-100,100)
b = drawing('简单',-60,30)
c = drawing('困难',-60,-40)
turtle.mainloop()





